package com.cndw.kungfu.dao;

import java.util.List;

import com.cndw.dao.jade.Identity;
import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;

import com.cndw.kungfu.domain.Event;

@DAO
public interface EventDao {

	public static final String TABLE = " kf_player_event ";

	public static final String FIELDS = " id,playerId,title,content,addTime,type,saved,readed,status";

	public static final String NoContentFIELDS = " id,playerId,title,addTime,type,saved,readed,status";
	
	public static final String ContentFIELDS = " id,playerId,title,content,addTime,type,saved,readed,status";

	@SQL("INSERT INTO " + TABLE + " (" + FIELDS + ") VALUES(:e.id,:e.playerId,:e.title,:e.content,:e.addTime,:e.type,:e.saved,:e.readed,:e.status) ")
	public Identity insert(@SQLParam("e") Event event);

	@SQL("select " + NoContentFIELDS + " from " + TABLE + " where playerId=:pid and status=0 #if(:type>0){ and type=:type } order by id desc limit :offset,:limit ")
	public List<Event> select(@SQLParam("pid") long playerId, @SQLParam("type") int type, @SQLParam("offset") int offset, @SQLParam("limit") int limit);
	
	@SQL("select " + ContentFIELDS + " from " + TABLE + " where playerId=:pid and status=0 #if(:type>0){ and type=:type } order by id desc limit :offset,:limit ")
	public List<Event> selectContent(@SQLParam("pid") long playerId, @SQLParam("type") int type, @SQLParam("offset") int offset, @SQLParam("limit") int limit);

	@SQL("select count(*) from " + TABLE + " where playerId=:pid and status=0 #if(:type>0){ and type=:type }  ")
	public int getCount(@SQLParam("pid") long playerId, @SQLParam("type") int type);

	@SQL("select " + FIELDS + " from " + TABLE + " where playerId=:1 and id=:2  ")
	public Event getEvent(long playerId, int id);

	@SQL("update " + TABLE + " set readed = 1 where playerId =:playerId and id in (:ids) ")
	public void signReaded(@SQLParam("playerId") long playerId, @SQLParam("ids") int[] ids);

	@SQL("update " + TABLE + " set readed = 1 where playerId =:playerId and id=:id ")
	public void signReaded(@SQLParam("playerId") long playerId, @SQLParam("id") int id);

	@SQL("update " + TABLE + " set saved = 1 where playerId =:playerId and id =:id ")
	public void signSaved(@SQLParam("playerId") long playerId, @SQLParam("id") int id);

	@SQL("update " + TABLE + " set status = 1 where playerId =:playerId and id =:id ")
	public void signDel(@SQLParam("playerId") long playerId, @SQLParam("ids") int id);

	@SQL("update " + TABLE + " set status = 1 where playerId =:playerId and id in (:ids) ")
	public void signDel(@SQLParam("playerId") long playerId, @SQLParam("ids") int[] ids);

	@SQL("update " + TABLE + " set status = 1 where playerId =:playerId #if(:type>0){ and type=:type } and status=0")
	public void delAll(@SQLParam("playerId") long playerId, @SQLParam("type") int type);

	@SQL("delete from "+TABLE+" where addTime<=UNIX_TIMESTAMP()-20*86400 limit 20000  ")
	public void clearData20();

	@SQL("update " + TABLE + " set status = 1 where status=0 and readed=1 and addTime<=UNIX_TIMESTAMP()-5*86400 limit 500 ")
	public void clearData5();

}
